#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void STInit(ST* pst)
{
	assert(pst);
	pst->top = NULL;
	pst->size = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);
	while (pst->top)
	{
		Node* next = pst->top->next;
		free(pst->top);
		pst->top = next;
	}
	pst->size = 0;
}

void STPush(ST* pst, STDataType x)
{
	assert(pst);
	Node* newcode = (Node*)malloc(sizeof(Node));
	if (newcode == NULL)
	{
		perror("malloc fail!\n");
		exit(1);
	}
	newcode->data = x;
	newcode->next = pst->top;
	pst->top = newcode;
	pst->size++;
}

void STPop(ST* pst)
{
	assert(pst && pst->top);
	Node* next = pst->top->next;
	free(pst->top);
	pst->top = next;
	pst->size--;
}

STDataType STTop(ST* pst)
{
	assert(pst && pst->top);
	return pst->top->data;
}

bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == NULL;
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->size;
}